package net.xiduth.utils.object;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

import net.xiduth.game.World;
import net.xiduth.game.WorldObject;
import net.xiduth.utils.FileUtilities;

/**
 * The class that handles deletion of World Objects in from JVM runtime.
 * 
 * @author Tyluur <ItsTyluur@Gmail.com>
 * @since 2012-07-27
 */
public final class ObjectRemoval {
	
	/** The arraylist of the objects which will be removed from the world globally. */
	private static final ArrayList<WorldObject> objects = new ArrayList<WorldObject>();
	
	/** The location of the files to remove objects from. */
	private static final String FILE_LOCATION = "./data/objectremoval.txt";
	
	/** The instance of the logger in this class. */
	private static final Logger logger = Logger.getLogger(ObjectRemoval.class.getCanonicalName());
	
	public static void init() {
		try {
			for (String lines : FileUtilities.readFile(FILE_LOCATION)) {
				if (lines.startsWith("//"))
					continue;
				String[] data = lines.split(" ");
				int id = Integer.parseInt(data[0]), x = Integer
						.parseInt(data[1]), y = Integer.parseInt(data[2]), z = Integer
						.parseInt(data[3]), type = Integer.parseInt(data[4]), rotation = Integer
						.parseInt(data[5]);
				WorldObject worldObject = new WorldObject(id, type, rotation, x, y, z);
				objects.add(worldObject);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (WorldObject object : objects) {
			if (object == null) continue;
			World.removeObject(object, true);
		}
		logger.info("Removed " + objects.size() + " world objects.");
	}

}